<template>
    <div class="page">
        <div class="chat">
            <!-- 消息列表 -->
            <div class="scroll-container">
                <div v-for="item in list" :key="item.id" :class="['chat-message', `chat-message-${item.role}`]">
                    <div class="chat-content">
                        <div>{{ item.content }}</div>
                    </div>
                </div>
            </div>
        </div>
        <div class="message">
            <!-- 聊天内容的数据绑定 -->
            <input v-model="content" @input="onInput" placeholder="请输入聊天内容..." class="message-input" />
            <button @click="send" class="send-button">发送</button>
        </div>
    </div>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted, watch } from 'vue';
import axios from 'axios';

interface ChatMessage {
    id: number;
    content: string;
    role: 'me' | 'server';
}

export default defineComponent({
    setup() {
        const content = ref('');
        const list = ref<ChatMessage[]>([]);
        const lastId = ref<number>(0);

        // 初始化默认消息
        const initMessages = () => {
            list.value.push({
                id: lastId.value++,
                content: '欢迎来到AI小助手！这里可以帮助您解决问题。',
                role: 'server'
            });
        };

        const onInput = (event: Event) => {
            content.value = (event.target as HTMLInputElement).value;
        };

        const send = async () => {
            if (!content.value.trim()) {
                alert('消息不能为空');
                return;
            }

            try {
                const response = await axios.post('http://127.0.0.1:5000/send_message', {
                    content: content.value
                });

                if (response.status === 200) {
                    list.value.push({
                        id: lastId.value++,
                        content: content.value,
                        role: 'me'
                    });
                    list.value.push({
                        id: lastId.value++,
                        content: response.data.message,
                        role: 'server'
                    });

                    content.value = '';
                } else {
                    alert('发送消息失败');
                }
            } catch (error) {
                console.error("发送消息失败: ", error);
                alert('发送消息失败');
            }
        };

        watch(list, () => {
            const container = document.querySelector('.scroll-container');
            if (container) {
                container.scrollTop = container.scrollHeight;
            }
        }, { immediate: true });

        onMounted(() => {
            // 页面加载时初始化消息
            initMessages();
        });

        return {
            content,
            list,
            send,
            onInput
        };
    }
});
</script>

<style scoped>
.page {
    background-color: #f0f0f0;
    height: 100vh;
    display: flex;
    flex-direction: column;
    font-family: Arial, sans-serif;
}

.chat {
    flex: 1;
    padding: 20px;
    background-color: #ffffff;
    overflow: visible;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    border-radius: 10px;
    margin: 10px;
}

.scroll-container {
    display: flex;
    flex-direction: column;
    height: 100%;
}

.chat-message {
    display: flex;
    margin-bottom: 15px;
    transition: background-color 0.3s ease;
}

.chat-content {
    padding: 12px 16px;
    border-radius: 12px;
    word-break: break-word;
    max-width: 80%;
    margin-left: 20px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.chat-message-me .chat-content {
    background-color: #007bff;
    color: #ffffff;
    align-self: flex-end;
}

.chat-message-server .chat-content {
    background-color: #f1f1f1;
    color: #333;
    align-self: flex-start;
    position: relative;
}

.chat-message-server .chat-content::before {
    content: '🤖';
    position: absolute;
    left: -30px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 24px;
}

.message {
    display: flex;
    align-items: center;
    padding: 10px;
    background-color: #ffffff;
    border-top: 1px solid #ddd;
    box-shadow: 0 -2px 4px rgba(0, 0, 0, 0.1);
}

.message-input {
    flex: 1;
    padding: 12px;
    border-radius: 20px;
    border: 1px solid #ccc;
    margin-right: 12px;
    font-size: 16px;
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
}

.send-button {
    padding: 10px 20px;
    border: none;
    background-color: #007bff;
    color: #ffffff;
    border-radius: 20px;
    cursor: pointer;
    font-size: 16px;
    transition: background-color 0.3s ease;
}

.send-button:hover {
    background-color: #0056b3;
}
</style>
